package cn.itcast.hiss.process.activiti.handler.task;

import cn.hutool.core.util.StrUtil;
import cn.itcast.hiss.api.client.task.AnyJumpTask;
import cn.itcast.hiss.cmd.handler.CmdHandler;
import cn.itcast.hiss.handler.HandlerIdClientEnum;
import cn.itcast.hiss.message.Message;
import cn.itcast.hiss.message.MessageContext;
import cn.itcast.hiss.message.sender.task.AnyJumpTaskMessage;
import cn.itcast.hiss.process.activiti.service.ActivitiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 任务驳回
 */
@Component
public class Task_RollBackTaskHandler implements CmdHandler<AnyJumpTaskMessage> {

    @Autowired
    ActivitiService activitiService;

    @Override
    public void invoke(Message params, MessageContext messageContext) {
        AnyJumpTask palyload = (AnyJumpTask) params.getPalyload();
        if(StrUtil.isEmpty(palyload.getOperatorName())){
            palyload.setOperatorName("驳回");
        }
        String jumpTaskOrActivityId = palyload.getJumpTaskOrActivityId();
        boolean first = "#start".equals(jumpTaskOrActivityId);
        if("#start".equals(jumpTaskOrActivityId)||"#prev".equals(jumpTaskOrActivityId)){
            palyload.setJumpTaskOrActivityId(null);
        }
        activitiService.rollBackTask((AnyJumpTaskMessage)params,messageContext,first);
    }

    @Override
    public String getId() {
        return HandlerIdClientEnum.TASK_ROLLBACK_TASK.getId();
    }
}
